﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Command;
using Entity;

namespace DAL
{
    public class T_Order
    {
        //查询所有省
        public static SqlDataReader Area()
        {
            string sql = string.Format("select * from Area where parentID = 0");
            return DBHelper.Getdata(sql);
        }

        //查询市
        public static SqlDataReader City(string i)
        {
            string sql = string.Format("select * from Area where parentID = '{0}'", i);
            return DBHelper.Getdata(sql);
        }

        //查询县
        public static SqlDataReader County(string i)
        {
            string sql = string.Format("select * from Area where parentID = '{0}'", i);
            return DBHelper.Getdata(sql);
        }

        //添加地址
        public static bool AddAddress(ShoppingAddress ads)
        {
            string sql = string.Format("insert ShoppingAddress values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", ads.UserID, ads.Saveid, ads.Cityid, ads.Countyid, ads.AddressValue, 2, ads.Name, ads.phone);
            return DBHelper.Adddata(sql) > 0;
        }

        //获取用户的地址表
        public static SqlDataReader Address(int id)
        {
            string sql = string.Format("select * from ShoppingAddress where UserID = '{0}' and AddressState<> 0 and AddressState<> 1", id);
            return DBHelper.Getdata(sql);
        }

        //查询详细地址
        public static SqlDataReader Cha(string i)
        {
            string sql = string.Format("select * from Area where codeID = '{0}'", i);
            return DBHelper.Getdata(sql);
        }

        //查询默认地址
        public static SqlDataReader Cham(int i)
        {
            string sql = string.Format("select * from ShoppingAddress where UserID = '{0}' and  AddressState = 1", i);
            return DBHelper.Getdata(sql);
        }

        //查询选中地址
        public static SqlDataReader Ucha(int i)
        {
            string sql = string.Format("select * from ShoppingAddress where AddressId = '{0}'", i);
            return DBHelper.Getdata(sql);
        }

        //修改用户地址表
        public static bool UpdataAddress(ShoppingAddress sh)
        {
            string sql = string.Format("update ShoppingAddress set Saveid = '{0}',Cityid = '{1}',Countyid = '{2}',AddressValue = '{3}',Name = '{4}',phone = '{5}' where AddressId = '{6}'", sh.Saveid, sh.Cityid, sh.Countyid, sh.AddressValue, sh.Name, sh.phone, sh.AddressId);
            return DBHelper.Adddata(sql) > 0;
        }

        //删除用户地址
        public static bool DelAddress(int id)
        {
            string sql = string.Format("DELETE FROM ShoppingAddress where AddressId='{0}'", id);
            return DBHelper.Adddata(sql) > 0;
        }

        //设置默认地址
        public static bool Addressm(int id)
        {
            string sql = string.Format("exec Addressm {0}", id);
            return DBHelper.Adddata(sql) > 0;
        }

        //生成订单
        public static bool OrderInfo(OrderInformatica oi)
        {
            string sql = string.Format("insert OrderInformatica values('{0}','{1}','{2}','{3}','{4}','{5}')", oi.OrderId, oi.OrderDate, oi.OrderState, oi.UserId, oi.OrderAmount, oi.OrderAddress);
            return DBHelper.Adddata(sql) > 0;
        }

        //添加商品订单
        public static bool AddOrder(List<OrderDetails> odeta, OrderInformatica oinfo)
        {
            bool p = false;
            string sql2 = string.Format("INSERT INTO OrderInformatica VALUES('{0}','{1}','{2}','{3}','{4}','{5}')", oinfo.OrderId, oinfo.OrderDate, oinfo.OrderState, oinfo.UserId, oinfo.OrderAmount, oinfo.OrderAddress);
            p = DBHelper.Adddata(sql2) > 0;
            foreach (OrderDetails o in odeta)
            {
                string sql = string.Format("INSERT INTO OrderDetails values('{0}','{1}','{2}','{3}')", o.OrderId, o.BookId, o.Count, o.BookPrice);
                if (DBHelper.Adddata(sql) <= 0)
                {
                    p = false;
                }
            }
            return p;
        }

        //查询购物详情
        public static SqlDataReader OrderInfo(string[] i)
        {
            string s = "";
            foreach (string a in i)
            {
                s +="'"+a+"',";
            }
            s = s.TrimEnd(',');
            string sql = string.Format("select * from CartItem where Id in({0})", s);
            return DBHelper.Getdata(sql);
        }

    }
}
